import {
  GetSystemCpuUsed,
  GetSystemInfo,
  GetSystemMemUsed,
  GetSystemNetworkLoad
} from '@shared/types'
import { contextBridge, ipcRenderer } from 'electron'

if (!process.contextIsolated) {
  throw new Error('contextIsolation must be enabled in the BrowserWindow')
}

const electron = {
  closeFocusedWindow: () => ipcRenderer.invoke('closeFocusedWindow'),
  minimizeFocusedWindow: () => ipcRenderer.invoke('minimizeFocusedWindow'),
  maximizeFocusedWindow: () => ipcRenderer.invoke('maximizeFocusedWindow')
}

const context = {
  getSystemInfo: (...args: Parameters<GetSystemInfo>) =>
    ipcRenderer.invoke('getSystemInfo', ...args),
  getSystemCpuUsed: (...args: Parameters<GetSystemCpuUsed>) =>
    ipcRenderer.invoke('getSystemCpuUsed', ...args),
  getSystemMemUsed: (...args: Parameters<GetSystemMemUsed>) =>
    ipcRenderer.invoke('getSystemMemUsed', ...args),
  getSystemNetworkLoad: (...args: Parameters<GetSystemNetworkLoad>) =>
    ipcRenderer.invoke('getSystemNetworkLoad', ...args)
}

try {
  contextBridge.exposeInMainWorld('electron', electron)
  contextBridge.exposeInMainWorld('context', context)
} catch (error) {
  console.error(error)
}
